<template>
  <div class="app-container">
    <!-- Search Form -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="tName">
        <el-input
          v-model="queryParams.tName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- Action Buttons -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['keep:teacher:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['keep:teacher:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['keep:teacher:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['keep:teacher:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- Table with Truncated Content -->
    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="教练id" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="tName" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="简介" align="center" prop="tIntro">
        <template slot-scope="scope">
          <div v-if="scope.row.showFullIntro" v-html="scope.row.tIntro" class="intro-text full"></div>
          <div v-else class="intro-text truncated" v-html="getShortIntro(scope.row.tIntro)"></div>
          <!-- <el-button
            size="mini"
            type="text"
            @click="toggleIntro(scope.row)">
            {{ scope.row.showFullIntro ? '收起' : '展开' }}
          </el-button> -->
        </template>
      </el-table-column>
      <el-table-column label="场馆" align="center" prop="fitnessGym.gymName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_gym_open" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['keep:teacher:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['keep:teacher:remove']"
          >删除</el-button>

          <el-button type="text" size="mini" @click="manageShop(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

    <!-- 添加或修改教练管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="教练照片" prop="picture">
          <image-upload v-model="form.picture"/>
        </el-form-item>
        <el-form-item label="姓名" prop="tName">
          <el-input v-model="form.tName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择场馆">
          <el-col :span="24">
            <el-form-item>
              <el-select v-model="form.venuesId" placeholder="请选择场馆" style="width: 100%;">
                <el-option
                  v-for="gym in gymList"
                  :key="gym.id"
                  :label="gym.gymName"
                  :value="gym.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="签名" prop="single">
          <el-input v-model="form.single" placeholder="请输入签名" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_gym_open"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="简介" prop="goodsIntroduce" height="1000px">
          <div class="quill_div"><quill-editor v-model="form.tIntro"></quill-editor></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher } from "@/api/keep/teacher";
import { listGym } from "@/api/keep/gym";

export default {
  name: "Teacher",
  dicts: ['sys_gym_open', 'sys_user_sex'],
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      teacherList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tName: null,
        venuesId: null,
      },
      form: {},
      gymList: [],
      rules: {
        tName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // getList() {
    //   this.loading = true;
    //   c(this.queryParams).then(response => {
    //     this.teacherList = response.data.map(item => ({
    //       ...item,
    //       showFullIntro: false // Initialize with collapsed state
    //     }));
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    getList() {
      // listGym(this.queryParams).then(response => {
      // 	this.gymList = response.rows;
      // 	this.total = response.total;
      // 	this.loading = false;
      // });
          this.loading = true;
          listTeacher(this.queryParams).then(response => {
            this.teacherList = response.rows;
            console.log(this.teacherList);
            // this.gymList=response.rows
            // console.log(this.gymList)
            this.total = response.total;
            this.loading = false;
          });

        },
    toggleIntro(item) {
      item.showFullIntro = !item.showFullIntro;
    },
    getShortIntro(intro) {
      const maxLength = 100; // Maximum length for the short intro
      return intro.length > maxLength ? intro.substring(0, maxLength) + '...' : intro;
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        picture: null,
        tName: null,
        sex: null,
        phone: null,
        single: null,
        tIntro: null,
        venuesId: null,
        status: null,
        del: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教练管理";
    },
    /** 修改按钮操作 */
        handleUpdate(row) {
          listGym(this.queryParams).then(response => {
          	this.gymList = response.rows;
            console.log(this.gymList);
          	this.total = response.total;
          	this.loading = false;
          });
          this.reset();
          const id = row.id || this.ids
          getTeacher(id).then(response => {
            this.form = response.data;
            this.open = true;
            this.title = "修改教练管理";
          });
        },
    submitForm() {
      if (this.form.tIntro) {
        // Remove all <br> and <p> tags from the tIntro field
        this.form.tIntro = this.form.tIntro.replace(/<br\s*\/?>/gi, '').replace(/<\/?p[^>]*>/gi, '');
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacher(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeacher(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除教练管理编号为"' + ids + '"的数据项？').then(() => {
        return delTeacher(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.download('keep/teacher/export', {
        ...this.queryParams
      }, `teacher_${new Date().getTime()}.xlsx`);
    },
    manageShop(row) {
      this.$router.push({ path: `/teacherInfo`, query: { id: row.id } });
    },
    handleSizeChange(newSize) {
      this.queryParams.pageSize = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.queryParams.pageNum = newPage;
      this.getList();
    }
  },
};
</script>

<style scoped>
.intro-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-height: 20px; /* Limit the height to make sure it does not overflow */
  line-height: 20px; /* Ensures consistent line height */
}

.intro-text.full {
  white-space: normal;
  max-height: none;
}

.intro-text img {
  display: none; /* Hide images in the intro text */
}

.el-table .el-table__body-wrapper {
  overflow: hidden; /* Ensure the table layout is preserved */
}
</style>
